package com.vtron.dbase.datasource;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 数据监控器
 * @date : 2022/2/15 17:44
 * @author: 忻龙彪
 */
public class MonitorConfig {
    @Bean
    public ServletRegistrationBean statViewServlet() {
        // StatViewServlet 配置后台监控
        ServletRegistrationBean bean = new ServletRegistrationBean(new
                StatViewServlet(), "/druid/*");
        HashMap<String, String> map = new HashMap<String, String>(16);
        //后台的登录用户名和密码
        map.put("loginUsername", "admin");
        map.put("loginPassword", "123456");
        // 访问权限
        //只允许本机访问
        // map.put("allow","localhost");
        // 所有人都可以访问
        map.put("allow", "");
        // deny拒绝访问
        // ip会被拒绝访问
        // map.put("deny","192.168.1.1");
        //设置servlet的初始化参数
        bean.setInitParameters(map);
        return bean;
    }

    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        // 配置内容
        // 配置哪些请求可以被过滤！
        HashMap<String, String> map = new HashMap<String, String>(16);
        map.put("exclusions", "*.js,*.css,/druid/*");
        bean.setInitParameters(map);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}
